package misc

import (
	"github.com/astaxie/beego/orm"
)

func AddOne(table, fields string, where map[string]string) (int64, error) {
	//o := models.GetDefaultDB()
	o := orm.NewOrm()
	err := o.Using("default")
	if err != nil {
		return 0, err
	}
	query := "UPDATE " + table + " SET " + fields + " = " + fields + "+1"

	i := 0
	for k, v := range where {
		if i == 0 {
			query = query + " WHERE " + k + " = \"" + v + "\""
		} else {
			query = query + " AND " + k + " = \"" + v + "\""
		}
		i++
	}

	res, err := o.Raw(query).Exec()
	num, err := res.RowsAffected()
	return num, err
}

func MinusOne(table, fields string, where map[string]string) (int64, error) {
	//o := models.GetDefaultDB()
	o := orm.NewOrm()
	err := o.Using("default")
	if err != nil {
		return 0, err
	}
	query := "UPDATE " + table + " SET " + fields + " = " + fields + "-1"

	i := 0
	for k, v := range where {
		if i == 0 {
			query = query + " WHERE " + k + " = \"" + v + "\""
		} else {
			query = query + " AND " + k + " = \"" + v + "\""
		}
		i++
	}

	res, err := o.Raw(query).Exec()
	num, err := res.RowsAffected()
	return num, err
}
